package com.xpchenfrank.test.duoxiancheng;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author xpchen
 * @time 2018/4/27 15:32
 * @Description
 * @copyright Wuxi Yazuo ,Ltd.copyright 2015-2025
 */
public class Tread2 {

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);

        final CountDownLatch latch = new CountDownLatch(5);

        List<Future> list = new ArrayList<Future>();

        for (int i = 0; i < 20; i++) {
            /*service.execute(new Runnable() {
                public void run() {
                    System.out.println("hello");
                    latch.countDown();
                }
            });*/

            Future<Integer> future = service.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println("hello");
                    latch.countDown();
                    return 123;
                }
            });
            list.add(future);
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            //e.printStackTrace();
        }

        for (Future<String> future : list) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        service.shutdownNow();
        System.out.println("over!!");
    }
}

